<template>
  <div class="app-container">
    <el-form  @submit.native.prevent :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="查询时间" prop="time">
        <el-date-picker clearable
          v-model="queryParams.time"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择查询时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="所在单位" prop="unit">-->
<!--        <el-input-->
<!--          v-model="queryParams.unit"-->
<!--          placeholder="请输入所在单位"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="用途" prop="purpose">
        <el-select v-model="queryParams.purpose" placeholder="请选择用途" clearable>
          <el-option
            v-for="dict in dict.type.use_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="详细用途" prop="detailPurpose">-->
<!--        <el-input-->
<!--          v-model="queryParams.detailPurpose"-->
<!--          placeholder="请输入详细用途"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="分类号" prop="classNumber">
        <el-input
          v-model="queryParams.classNumber"
          placeholder="请输入分类号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="案卷卷册" prop="volume">
        <el-input
          v-model="queryParams.volume"
          placeholder="请输入案卷卷册"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="案卷编号" prop="serial">
        <el-input
          v-model="queryParams.serial"
          placeholder="请输入案卷编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="档案内容" prop="archiveDetails">-->
<!--        <el-input-->
<!--          v-model="queryParams.archiveDetails"-->
<!--          placeholder="请输入档案内容"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="联系电话" prop="number">-->
<!--        <el-input-->
<!--          v-model="queryParams.number"-->
<!--          placeholder="请输入联系电话"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备注" prop="ps">-->
<!--        <el-input-->
<!--          v-model="queryParams.ps"-->
<!--          placeholder="请输入备注"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-row :gutter="10" class="mb8">
        <el-col :span="24">
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
              >查阅登记</el-button>
<!--              v-hasPermi="['system:register:add']"-->
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
              >补充信息</el-button>
<!--              v-hasPermi="['system:register:edit']"-->
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
              >删除记录</el-button>
<!--              v-hasPermi="['system:register:remove']"-->
            </el-col>
                  <el-col :span="1.5">
                    <el-button
                      type="warning"
                      plain
                      icon="el-icon-download"
                      size="mini"
                      @click="handleExport"

                    >导出</el-button>
<!--                    v-hasPermi="['system:register:export']"-->
                  </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>
          <el-table v-loading="loading" :data="registerList" @selection-change="handleSelectionChange" show-summary :summary-method="getSummaries">
            <el-table-column type="selection" width="55" align="center" />
            <!--      <el-table-column label="${comment}" align="center" prop="id" />-->
            <el-table-column label="查询时间" align="center" prop="time">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="姓名" align="center" prop="name" />
            <el-table-column label="所在单位" align="center" prop="unit" />
            <el-table-column label="用途" align="center" prop="purpose">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.use_type" :value="scope.row.purpose"/>
              </template>
            </el-table-column>
            <el-table-column label="详细用途" align="center" prop="detailPurpose" />
            <el-table-column label="分类号" align="center" prop="classNumber" />
            <el-table-column label="案卷卷册" align="center" prop="volume" />
            <el-table-column label="查询人数" align="center" prop="people" />
            <el-table-column label="案卷编号" align="center" prop="serial" />
            <el-table-column label="档案内容" align="center" prop="archiveDetails" />
            <el-table-column label="联系电话" align="center" prop="number" />
            <el-table-column label="备注" align="center" prop="ps" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                >修改</el-button>
<!--                v-hasPermi="['system:register:edit']"-->
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                >删除</el-button>
<!--                v-hasPermi="['system:register:remove']"-->
              </template>
            </el-table-column>
          </el-table>
              <!-- 右侧div -->
              <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
              />
        </el-col>

    </el-row>

    <!-- 添加或修改档案查阅管理功能对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form  @submit.native.prevent ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="查询时间" prop="time">
          <el-date-picker clearable
            v-model="form.time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择查询时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="所在单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入所在单位" />
        </el-form-item>
        <el-form-item label="用途" prop="purpose">
          <el-select v-model="form.purpose" placeholder="请选择用途">
            <el-option
              v-for="dict in dict.type.use_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="详细用途" prop="detailPurpose">
          <el-input v-model="form.detailPurpose" placeholder="请输入详细用途" />
        </el-form-item>
        <el-form-item label="分类号" prop="classNumber">
          <el-input v-model="form.classNumber" placeholder="请输入分类号" />
        </el-form-item>
        <el-form-item label="案卷卷册" prop="volume">
          <el-input v-model="form.volume" placeholder="请输入案卷卷册" />
        </el-form-item>
        <el-form-item label="查询人数" prop="people">
          <el-input  oninput="value=value.replace(/[^\d]/g,'')" v-model="form.people" placeholder="请输入查询人数" />
        </el-form-item>
        <el-form-item label="案卷编号" prop="serial">
          <el-input v-model="form.serial" placeholder="请输入案卷编号" />
        </el-form-item>
        <el-form-item label="档案内容" prop="archiveDetails">
          <el-input v-model="form.archiveDetails" placeholder="请输入档案内容" />
        </el-form-item>
        <el-form-item label="联系电话" prop="number">
          <el-input  oninput="value=value.replace(/[^\d]/g,'')" v-model="form.number" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="备注" prop="ps">
          <el-input v-model="form.ps" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRegister, getRegister, delRegister, addRegister, updateRegister,sumPeople,sumVolume } from "@/api/system/register";

export default {
  name: "Register",
  dicts: ['use_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      sumPeople:0,
      sumVolume:0,
      // 档案查阅管理功能表格数据
      registerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        time: null,
        name: null,
        unit: null,
        purpose: null,
        people:null,
        detailPurpose: null,
        classNumber: null,
        volume: null,
        serial: null,
        archiveDetails: null,
        number: null,
        ps: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        time: [
          { required: true, message: "查询时间不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        unit: [
          { required: true, message: "所在单位不能为空", trigger: "blur" }
        ],
        purpose: [
          { required: true, message: "用途不能为空", trigger: "change" }
        ],
        people: [
          { required: true, message: "查询人数不能为空", trigger: "blur" }
        ],
        // detailPurpose: [
        //   { required: true, message: "详细用途不能为空", trigger: "blur" }
        // ],
        // classNumber: [
        //   { required: true, message: "分类号不能为空", trigger: "blur" }
        // ],
        // volume: [
        //   { required: true, message: "案卷卷册不能为空", trigger: "blur" }
        // ],
        // serial: [
        //   { required: true, message: "案卷编号不能为空", trigger: "blur" }
        // ],
        archiveDetails: [
          { required: true, message: "档案内容不能为空", trigger: "blur" }
        ],
        number: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询档案查阅管理功能列表 */
    getList() {
      sumPeople().then(response =>{
        this.sumPeople=response;
      });
      sumVolume().then(response =>{
        this.sumVolume=response;
      })
      this.loading = true;
      listRegister(this.queryParams).then(response => {
        this.registerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总计';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          sums[index] = '';
        } else {
          sums[index] = '';
        }
      });
      sums[7] =this.sumVolume+"卷";
      sums[8] =this.sumPeople+"人";
      console.log(sums)
      return sums;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        time: null,
        name: null,
        unit: null,
        purpose: null,
        people:null,
        detailPurpose: null,
        classNumber: null,
        volume: null,
        serial: null,
        archiveDetails: null,
        number: null,
        ps: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加档案查阅管理功能";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRegister(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改档案查阅管理功能";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRegister(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRegister(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除档案查阅管理功能编号为"' + ids + '"的数据项？').then(function() {
        return delRegister(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/register/export', {
        ...this.queryParams
      }, `register_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
